package deliciouse.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import deliciouse.biz.NewsBiz;
import deliciouse.biz.impl.NewsBizImpl;
import deliciouse.entity.News;

@WebServlet("/newslist")
public class NewsListServlet extends HttpServlet {
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		int pageNum = 1;
		if (request.getParameter("pageNum") != null && !request.getParameter("pageNum").isEmpty())
			pageNum = Integer.valueOf(request.getParameter("pageNum"));
		int pageSize = 6;
		int pageNumMax = 0;
		int total = 0;
		NewsBiz newsbiz = new NewsBizImpl();
		pageNumMax = newsbiz.MessageMaxRows();
		total = pageNumMax % pageSize == 0 ? (pageNumMax / pageSize) : (pageNumMax / pageSize + 1);
		List<News> list = newsbiz.fetchByDate(pageNum, pageSize);
		request.setAttribute("pageNum", pageNum);
		request.setAttribute("total", total);
		request.setAttribute("list", list);
		request.getRequestDispatcher("newslist.jsp").forward(request, response);
	}
}
